﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;

namespace DaveSexton.Labs.Wizards
{
	[CLSCompliant(false)]
	public sealed class FSharpLabProjectTemplateWizard : IWizard
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		public FSharpLabProjectTemplateWizard()
		{
		}
		#endregion

		#region Methods
		private static void SetItemType(ProjectItem item)
		{
			if (item != null && string.Equals(Path.GetExtension(item.Name), ".xaml"))
			{
				var properties = item.Properties;

				Contract.Assume(properties != null);

				var itemType = properties.Item("ItemType");

				Contract.Assume(itemType != null);

				itemType.Value = "Resource";
			}
		}
		#endregion

		#region IWizard Members
		public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
		{
		}

		public void ProjectFinishedGenerating(Project project)
		{
			Contract.Assume(project != null);

			var items = project.ProjectItems;

			Contract.Assume(items != null);

			foreach (var item in items.Cast<ProjectItem>())
			{
				SetItemType(item);
			}
		}

		public void RunFinished()
		{
		}

		public void BeforeOpeningFile(ProjectItem projectItem)
		{
		}

		public bool ShouldAddProjectItem(string filePath)
		{
			return true;
		}

		public void ProjectItemFinishedGenerating(ProjectItem projectItem)
		{
		}
		#endregion
	}
}